<?php
namespace app\agent\controller;
use think\Controller;
use think\Db;
use app\common\controller\AgentBaseController;

class Count extends AgentBaseController
{
	//统计
	public function user_form()
    {
    	$usersum = db('info_user')->where('mobile','<>','')->where('level',session('agent_user_id'))->count();
    	$this->assign('usersum',$usersum);
    	$usersum1 = db('info_user')->where('mobile','eq','')->where('level',session('agent_user_id'))->count();
    	$this->assign('usersum1',$usersum1);
    	return $this->fetch();
    }
    
    public function goods_form()
    {
    	if(request()->isPost()){
    		$param=input('post.');
			
			if(!empty($param['goods_no']))$where['a.goods_no'] = ['like','%'.$param['goods_no'].'%'];
			
			if(!empty($param['goods_name']))$where['a.goods_name'] = ['like','%'.$param['goods_name'].'%'];
    	}
		if(input('type_id'))$where['a.trade_id'] = input('type_id');
		$where['a.agent_id'] = session('agent_id');
		//获取当前店铺商品
		$goods_list = db('agent_goods')
						->alias('a')
						->field('a.total_money,a.id as goods_id,a.goods_no,a.goods_name,a.sale_price,a.stock,a.goods_state,a.sales,a.addtime,b.id,b.type_name')
						->join('agent_type b','a.trade_id = b.id')
						->where($where)
						->order('a.sales desc')
						->paginate(15);
		$this->assign('goods_list',$goods_list);
		
		//获取商品分类类型
		$goods_type = db('agent_type')->where('agent_id',session('agent_id'))->select();
		$this->assign('goods_type',$goods_type);
		$this->assign('empty','<span class="empty">没有数据</span>');
		
    	return $this->fetch();
    }
    
    
    
    public function summary()
    {
    	$where['agent_id']=session("agent_id");	
    	$where['state']=2;
		$list=db("log_paylog")->where($where)->field("uid,balance,order_type,score,sy_type,payment_type")->select();
		$data['mem_orders']=0;//会员消费订单笔数
		$data['mem_orders_ms']=0;//会员消费总额
		$data['sk_orders']=0;//散客消费订单笔数
		$data['sk_orders_ms']=0;//散客消费金额
		$data['sm_orders']=0;//扫码支付订单笔数
		$data['sm_orders_ms']=0;//扫码支付总额
		$data['glj_ms']=0;//鼓励金总额
		$data['glj_zs']=0;//鼓励金赠送
		$data['glj_dk']=0;//鼓励金抵扣
		$data['sc_orders']=0;//商城订单
		$data['sc_orders_ms']=0;//商城订单金额
		if($list){
			foreach($list as $k=>$v){
				if($v['payment_type']==1){
					$data['glj_zs']+=$v['score'];
				}elseif($v['payment_type']==2){
					$data['glj_dk']+=$v['score'];
				}
				$data['glj_ms']+=$v['score'];
				if($v['uid']){
					$data['mem_orders']++;
					$data['mem_orders_ms']+=$v['balance'];
				}else{
					$data['sk_orders']++;
					$data['sk_orders_ms']+=$v['balance'];
				}
				 if($v['order_type']==1){
				 	$data['sc_orders']++;
					$data['sc_orders_ms']+=$v['balance'];
				 }elseif($v['order_type']==2){
				 	$data['sm_orders']++;
					$data['sm_orders_ms']+=$v['balance'];
				 }elseif($v['order_type']==6&&$v['uid']){
				 	$data['sm_orders']++;
					$data['sm_orders_ms']+=$v['balance'];
				 }
			}
		}
		$mp=array('jz'=>$data['sk_orders_ms'],"sm"=>$data['sm_orders_ms'],'sc'=>$data['sc_orders_ms']);
		$this->assign('mp',json_encode($mp));
		$this->assign("data",$data);
    	return $this->fetch();
    }
    public function detailed()
    {
		if(input('begin_time') && input('end_time')){
			$where['a.add_time'] = ['between time',[input('begin_time'),input('end_time')]];
		}
		if(input('store_name'))$where['a.chain_id'] = ['like','%'.input('store_name').'%'];
		if(input('syylist'))$where['a.clerk_id'] = input('syylist');
		if(input('pay_type'))$where['a.pay_type'] = input('pay_type');
		if(input('sy_type'))$where['a.sy_type'] = input('sy_type');
		if(input('state')){
			$where['a.state'] = input('state');
		}else{
			$where['a.state'] = 2;
		}
		if(input('order_sn'))$where['a.order_sn'] = ['like','%'.input('order_sn').'%'];

    	$where['a.agent_id'] = session('agent_id');	
		$where['a.order_type'] = ['not in','3,4,5'];		
    	$paylog = db('log_paylog')
    	->alias('a')
		->join('agent_store w','a.agent_id = w.agent_id and a.chain_id = w.id','LEFT')
		->field('a.*,w.store_name')
    	->where($where)
		->order('a.add_time desc')
    	->paginate(10,false,['query'=>request()->param()]);
		$this->assign('paylog',$paylog);

		//消费金额
		$xfprice = db('log_paylog')->alias('a')->where($where)->sum('a.origin_price');
		$this->assign('xfprice',$xfprice);
		
		//实际金额
		$sjprice = db('log_paylog')->alias('a')->where($where)->sum('a.balance');
		$this->assign('sjprice',$sjprice);
		
		//赠送
		$zsscore = db('log_paylog')->alias('a')->where($where)->where('a.payment_type',1)->sum('a.score');
		$this->assign('zsscore',$zsscore);
		
		//抵扣
		$dkscore = db('log_paylog')->alias('a')->where($where)->where('a.payment_type',2)->sum('a.score');
		$this->assign('dkscore',$dkscore);
		
		//获取门店ID
		$agent_store = db('agent_store')->where('agent_id',session('agent_id'))->select();
		$this->assign('agent_store',$agent_store);
		
		//获取收银员
		$clerk_list = db('agent_clerk')->where('agent_id',session('agent_id'))->select();
		$this->assign('clerk_list',$clerk_list);
		$this->assign('empty','<span class="empty">没有数据</span>');
		
    	return $this->fetch();
    }

 	//明细导出
    public function detail_dc(){
		if(input('begin_time') && input('end_time')){
			$where['a.add_time'] = ['between time',[input('begin_time'),input('end_time')]];
		}
		if(input('store_name'))$where['a.chain_id'] = ['like','%'.input('store_name').'%'];
		if(input('syylist'))$where['a.clerk_id'] = input('syylist');
		if(input('pay_type'))$where['a.pay_type'] = input('pay_type');
		if(input('sy_type'))$where['a.sy_type'] = input('sy_type');
		if(input('state')){
			$where['a.state'] = input('state');
		}else{
			$where['a.state'] = 2;
		}
		if(input('order_sn'))$where['a.order_sn'] = ['like','%'.input('order_sn').'%'];

    	$where['a.agent_id'] = session('agent_id');	
		$where['a.order_type'] = ['not in','3,4,5'];	
    	$list = db('log_paylog')
    	->alias('a')
		->join('agent_store w','a.agent_id = w.agent_id and a.chain_id = w.id','LEFT')
		->field('a.*,w.store_name')
    	->where($where)
		->order('a.add_time desc')
    	->select();

		$str ="订单号,日期,门店,项目,支付方式,消费金额,消费金额,鼓励金"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
        	$order_sn = iconv('utf-8','gbk',$row['order_sn'].'	');
			$add_time = iconv('utf-8','gbk',$row['add_time'].'	');
			$store_name = iconv('utf-8','gbk',$row['store_name']); 
			if($row['sy_type']==1){
				$syname = '记账';
			}elseif($row['sy_type']==2) {
				$syname = '扫码';
			}else{
				$syname = '商品';
			}
			$syname = iconv('utf-8','gbk',$syname); 
			
			if($row['pay_type']==1){
				$payname = '余额';
			}elseif($row['pay_type']==2) {
				$payname = '微信';
			}elseif($row['pay_type']==3) {
				$payname = '支付宝';
			}else{
				$payname = '银联';
			}
			$pay_type = iconv('utf-8','gbk',$payname);
			$xfprice = iconv('utf-8','gbk','	'.$row['origin_price']);
			$sjprice = iconv('utf-8','gbk','	'.$row['balance']);
			if($row['payment_type']==1){
				$oprate = '+';
			}elseif($row['payment_type']==2){
				$oprate = '-';
			}else{
				$oprate = '';
			}			
			$score = iconv('utf-8','gbk','	'.$oprate.$row['score']);
			$str .= $order_sn.",".$add_time.",".$store_name.",".$syname.",".$pay_type.",".$xfprice.",".$sjprice.",".$score.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='明细列表.csv';   //设置文件名  
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
	}

    public function syylist()
    {
    	//获取收银员
		$data = db('agent_clerk')->where('chain_id',input('id'))->select();
		return json(["data"=>$data]);
    }
    
    
    
    
    
}
